package com.dzl.goods;

import com.dzl.base.controller.BaseController;
import com.dzl.base.query.BaseQuery;
import com.dzl.constant.WelfareGoodsEnum;
import com.dzl.goods.pojo.WelfareGoodsClassifyEntryRel;
import com.dzl.goods.pojo.dto.WelfareGoodsClassifyEntryRelSaveDto;
import com.dzl.goods.pojo.query.WelfareGoodsManageQuery;
import com.dzl.goods.pojo.vo.WelfareGoodsClassifyEntryRelVo;
import com.dzl.goods.pojo.vo.WelfareGoodsManageVo;
import com.dzl.goods.service.WelfareGoodsClassifyEntryRelService;
import com.dzl.goods.service.WelfareGoodsService;
import com.dzl.util.EmptyUtils;
import com.dzl.util.JsonResult;
import com.dzl.util.PageUtils;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;

/**
 * 分类入口商品表(welfare_goods_classify_entry_rel)表控制层
 * @author liuguanjun
 * @date 2021-12-29 16:11:42
 */
@Slf4j
@RestController
@RequestMapping("/welfareGoodsClassifyEntry")
public class WelfareGoodsClassifyEntryRelController extends BaseController {

    @Autowired
    private WelfareGoodsService welfareGoodsService;
    @Autowired
    private WelfareGoodsClassifyEntryRelService service;

    /**
     * 查询分类入口设置商品列表
     * @param query 查询实体类
     * @author liuguanjun
     * @date 2021-12-29 16:46:56
     **/
    @GetMapping("getGoodsListRel")
    public PageUtils<WelfareGoodsClassifyEntryRelVo> getGoodsListManage(BaseQuery query) {
        PageInfo<WelfareGoodsClassifyEntryRelVo> pageInfo = service.getGoodsListClassifyEntryRelPageManager(query);
        return new PageUtils<>(pageInfo);
    }

    /**
     * 分类入口选择商品列表
     * @param query 查询实体类
     * @author liuguanjun
     * @date 2021-12-29 16:46:56
     **/
    @GetMapping("getGoodsListPage")
    public PageUtils<WelfareGoodsManageVo> getWelfareGoodsListPage(WelfareGoodsManageQuery query) {
        query.setRelClassifyEntry(2);//查所有商品不包含已选择的分类入口的商品
        query.setGoodsStatus(WelfareGoodsEnum.GOODS_STATUS_TYPE_SHELVES.getCode());
        query.setGoodsType(WelfareGoodsEnum.GOODS_TYPE_VIRTUAL.getCode());
        PageInfo<WelfareGoodsManageVo> pageInfo = welfareGoodsService.getWelfareGoodsListPage(query);
        return new PageUtils<>(pageInfo);
    }

    /**
     * 设置图片
     * @param saveDto imgUrl 分类入口图片
     * @author liuguanjun
     * @date 2021-12-29 17:09:22
     **/
    @PostMapping("setGoodsClassifyEntryImg")
    public JsonResult<Integer> setGoodsClassifyEntryImg(@RequestBody WelfareGoodsClassifyEntryRelSaveDto saveDto) {
        /*if (EmptyUtils.isEmpty(saveDto.getImgUrl())) {
            return JsonResult.paramErrWithMsg("分类入口图片不能传空");
        }*/
        return JsonResult.success(service.setGoodsClassifyEntryImg(saveDto.getImgUrl()));
    }

    /**
     * 设置名称
     * @param saveDto name 分类入口名称
     * @author liuguanjun
     * @date 2021-12-29 17:09:22
     **/
    @PostMapping("setGoodsClassifyEntryName")
    public JsonResult<Integer> setGoodsClassifyEntryName(@RequestBody WelfareGoodsClassifyEntryRelSaveDto saveDto) {
        if (EmptyUtils.isEmpty(saveDto.getName())) {
            return JsonResult.paramErrWithMsg("分类入口名称不能为空");
        }
        if(saveDto.getName().length()>25){
            return JsonResult.paramErrWithMsg("分类入口名称至多25个字符");
        }
        return JsonResult.success(service.setGoodsClassifyEntryName(saveDto.getName()));
    }

    /**
     * 修改分类入口商品排序
     * @param entity id主键id sort排序
     * @author liuguanjun
     * @date 2021-12-30 14:17:22
     **/
    @PostMapping("updateGoodsSort")
    public JsonResult<Integer> updateGoodsSort(@RequestBody WelfareGoodsClassifyEntryRel entity) {
        if (EmptyUtils.isEmpty(entity.getId())) {
            return JsonResult.paramErrWithMsg("分类入口商品id不能为空");
        }
        if (EmptyUtils.isEmpty(entity.getSort())) {
            return JsonResult.paramErrWithMsg("分类入口商品排序不能为空");
        }
        entity.setUpdateTime(new Date());
        return JsonResult.success(service.updateByPrimaryKeySelective(entity));
    }

    /**
     * 添加入口商品
     * @param saveDto goodsIds 选择商品id
     * @author liuguanjun
     * @date 2021-12-29 17:09:22
     **/
    @PostMapping("saveRelGoods")
    public JsonResult<Integer> saveRelGoods(@RequestBody WelfareGoodsClassifyEntryRelSaveDto saveDto) {
        if (EmptyUtils.isEmpty(saveDto.getGoodsIds())) {
            return JsonResult.paramErrWithMsg("商品id不能为空");
        }
        return JsonResult.success(service.saveRelGoods(saveDto.getGoodsIds()));
    }



    /**
     * 根据id删除分类入口信息
     * @author liuguanjun
     * @since 2021-12-29 17:09:22
     **/
    @GetMapping(value = "deleteById")
    public JsonResult<Integer> deleteById(Integer id) {
        if (EmptyUtils.isEmpty(id)) {
            return JsonResult.paramErrWithMsg("分类入口id不能为空");
        }
        Integer result =  service.deleteGoodsClassifyEntryById(id);
        return JsonResult.success(result);
    }

    /**
     * 获取分类入口详细信息
     * @author liuguanjun
     * @since 2021-12-29 17:09:22
     **/
    @GetMapping(value = "getGoodsClassifyEntryInfo")
    public JsonResult<Map> getGoodsInfo() {
        return JsonResult.success(service.getGoodsClassifyEntryInfo());
    }


}
